package com.example.demo.controller;

import com.example.demo.mapper.JobMapper;
import com.example.demo.pojo.Account;
import com.example.demo.pojo.Department;
import com.example.demo.pojo.Job;
import com.example.demo.pojo.Staff;
import com.example.demo.service.DepartmentService;
import com.example.demo.service.JobService;
import com.example.demo.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class StaffController {

    @Autowired
    private StaffService staffService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private JobService jobService;

    @RequestMapping("/emps")
    public String getAllList(Model model){
        model.addAttribute("emps", staffService.getAllTheStaffs());
        model.addAttribute("deps", departmentService.getAllTheDepartments());
        model.addAttribute("jobs",jobService.getAllJob());
        return "emp/list";
    }



    @GetMapping("/addemp")
    public String toAddemp(Model model) {
        model.addAttribute("departments",departmentService.getAllTheDepartments());
        model.addAttribute("jobs",jobService.getAllJob());
        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Staff staff) {
        staffService.addNewStaff(staff);
        System.out.println("添加员工"+staff);
        return "redirect:/emps";
    }

    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id")Integer id, Model model){
        Staff staff = staffService.getStaff(id);
        model.addAttribute("emp", staff);
        List<Job> allJob = jobService.getAllJob();
        model.addAttribute("jobs", allJob);
        System.out.println(allJob);
        model.addAttribute("departments",departmentService.getAllTheDepartments());
        return "emp/update";
    }

    @PostMapping("/updateEmp")
    public String updateEmp(Staff staff){
        staffService.updateStaff(staff);
        return "redirect:/emps";
    }

    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id")int id){
        staffService.deleteStaffById(id);
        return "redirect:/emps";
    }

    @PostMapping("/searchStaff")
    public String search(@RequestParam("conditionText") String conditionText,
                         @RequestParam("conditionType") String conditionType,
                         @RequestParam("conditionGender") String conditionGender,
                         @RequestParam("conditionJob") String conditionJob,
                         @RequestParam("conditionDepartment") String conditionDepartment,
                         Model model){
        model.addAttribute("emps", staffService.getStaffByCondition(conditionText,
                conditionType,
                conditionGender,
                conditionJob,
                conditionDepartment));
        model.addAttribute("deps", departmentService.getAllTheDepartments());
        model.addAttribute("jobs", jobService.getAllJob());
        model.addAttribute("conditionText",conditionText);
        return "emp/list";
    }

    @GetMapping("/per")
    public String toUpdatePer(Model model, HttpSession session){
        Staff loginuser = (Staff) session.getAttribute("loginuser");
        System.out.println("loginuser="+loginuser);
        Staff staff = staffService.getStaff(loginuser.getS_id());
        model.addAttribute("emp", staff);
        List<Job> allJob = jobService.getAllJob();
        return "staff/personInfo";
    }

    @PostMapping("/updatePer")
    public String updatePer(Staff staff, HttpSession session){
//        Account account = (Account) session.getAttribute("loginuser");
//        Staff staff1 = staffService.getStaff(account.getS_id());
        staffService.updateStaff(staff);
        return "redirect:/per";
    }
}
